package StudyCode.summary.zipfile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DecompressDataBaytesWithGzipAlgorithm {
	public static byte[] ungzip(byte[] data) throws IOException {
		if (data == null) {
			return data;
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(data);

		GZIPInputStream gis = null;
		try {
			gis = new GZIPInputStream(in);
			byte[] buffer = new byte[1024];
			int n;
			while ((n = gis.read(buffer)) >= 0) {
				out.write(buffer, 0, n);
			}
		} finally {
			if (gis != null) {
				gis.close();
			}
		}

		return out.toByteArray();
	}
}
